package com.itheima.filter;//package com.itheima.filter;
//
//import com.itheima.utils.JwtUtils;
//import jakarta.servlet.*;
//import jakarta.servlet.annotation.WebFilter;
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;
//import lombok.extern.slf4j.Slf4j;
//
//import java.io.IOException;
//
//@WebFilter(urlPatterns = "/*")
//@Slf4j
//public class myFilter implements Filter {
//    @Override
//    public void init(FilterConfig filterConfig) throws ServletException {
//        Filter.super.init(filterConfig);
//        log.info("拦截前");
//    }
//
//    @Override
//    public void destroy() {
//        Filter.super.destroy();
//        log.info("拦截后");
//    }
//
//    @Override
//    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//    log.info("放行前");
////    filterChain.doFilter(servletRequest,servletResponse);放行方法，不能随便放行
//        HttpServletRequest request = (HttpServletRequest) servletRequest;
//        HttpServletResponse response = (HttpServletResponse) servletResponse;
//
////        记得加return!!!!!!!!!!!!!
//        String uri = request.getRequestURI();
//        if (uri.contains("/login")) {
//            filterChain.doFilter(servletRequest,servletResponse);
//            return;
//        }
//
//        String token = request.getHeader("token");
//        if (token == null) {
//        log.info("如果没有令牌");
//        response.setStatus(401);
//        return;
//        }
//
////        上面一个登录直接放行，一个没有令牌，最后一种情况就是有令牌，继续验证令牌是否有效即可
//
////        解析失败会抛出异常，所以try catch
//        try {
//            JwtUtils.parseJWT(token);
////            解析成功，直接放行
//            filterChain.doFilter(servletRequest,servletResponse);
//        } catch (Exception e) {
//            log.info("令牌无效");
//            response.setStatus(401);
//            return;
//        }
//
//        log.info("放行后");
//    }
//}
